﻿/*
   Author: Tomer Paz
   games.com
   2007
*/
//the Player Viewer (Optional Abstract class, for future enhanced implementations)
package monopoly.views {
	
	import flash.display.Sprite;
	import fl.controls.Label;
	import fl.controls.TextArea;
	import flash.display.BlendMode;
	import flash.events.MouseEvent;
	import monopoly.Player.Player;
	import monopoly.views.PlayerStatusBox;
	import monopoly.views.BottomPanel;
	import flash.events.Event;
	import fl.transitions.Tween;
	import fl.transitions.easing.*;
	import fl.transitions.TweenEvent;
	import monopoly.IFS.GLOBALS;
	
	
	public class PlayerView extends Sprite
		{
		protected var Colors:Array = GLOBALS.PCOLORS;
		protected var _data:Player;
		protected var _StatusBox:PlayerStatusBox;
		protected var _myName:Label = new Label();
		
		protected const VIEWSIZE:Number = 20;
		
		public function PlayerView(Data:Player)
			{
			_data = Data;
			SetupNameLabel();
			draw(null);
			
			SetupStatusBox();
		
			// register for Player data changes
			_data.addEventListener(Event.CHANGE, draw);
			// register to First addition of player to view
			_data.addEventListener(Event.ADDED, draw);
			// register mouse-over events
			addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
			addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
			}
		public function get Data():Object
			{
			return _data;
			}
		public function get StatusBox():PlayerStatusBox
			{
			return _StatusBox;
			}

		protected function SetupNameLabel():void
			{
			_myName.move(0,VIEWSIZE);
			_myName.text = _data.Name;
			addChild(_myName);
			}
		protected function draw(event:Event):void
			{
			//var position:uint = _data.CurrentPosition;  => currently game-controller takes care of that
			
			/* set visibility */
			
			if (VisibleStatus() == false)
				return;
			
			/* move icon if Not first draw */
			
			if (null != event && (event.type == Event.CHANGE))
				{
				var ViewXTweenMove:Tween = new Tween(this, "x", None.easeOut, this.x, _data.X, .5, true);
				var ViewYTweenMove:Tween = new Tween(this, "y", None.easeOut, this.y, _data.Y, .5, true);
	
				ViewXTweenMove.start();
				ViewYTweenMove.start();
					
				// play horn sound
				_data.play_moveSound();
				}
			else	// first time
				{
				with (this.graphics) 
					{
					lineStyle(0, Colors[_data.ID - 1], 1, false, "normal", _data.Name);
					beginFill(Colors[_data.ID - 1], 0.5);
		         	drawEllipse(_data.X, _data.Y, VIEWSIZE, VIEWSIZE);
					endFill();
					}
//				ViewYTweenMove.addEventListener(TweenEvent.MOTION_FINISH,OnViewTeenComplete);
				_myName.x = _data.X;
				
				/* remove listener if this is ADDED event */
			
				_data.removeEventListener(Event.ADDED, draw);
				
				// play player sound
				_data.play_playerSound();
				}
			
			this.x = _data.X;
			this.y = _data.Y;

			this.blendMode = BlendMode.LAYER;
			this.alpha = 0.9;
			}
			
		protected function VisibleStatus():Boolean
			{
			this.visible = _data.VisibleStatus;
			return this.visible;
			}
/*
		protected function OnViewTeenComplete(event:Object)
			{
			with (this.graphics) 
					{
					clear();
					lineStyle(0, Colors[_data.ID - 1], 1, false, "normal", _data.Name);
					beginFill(Colors[_data.ID - 1], 0.5);
		         	drawEllipse(_data.X, _data.Y, VIEWSIZE, VIEWSIZE);
					endFill();
					}
			}
*/
		protected function mouseOverHandler(event:MouseEvent):void
			{
			if (event.currentTarget == this)
				{
				_data.play_playerSound();
		        _StatusBox.draw_statusBox();
				}
			}
		protected function mouseOutHandler(event:MouseEvent):void
			{
			if (event.currentTarget == this)
				{
		        _StatusBox.undraw_statusBox();
				_data.stop_playerSound();
				}
    		}
			
		protected function SetupStatusBox():void
			{
			_StatusBox = new PlayerStatusBox(_data);
			_StatusBox.load();
			
			_StatusBox.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
			_StatusBox.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
			_StatusBox.name = BottomPanel.BPANEL_CENTER;
			//addChild(_StatusBox);
			}
		}
}